﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Landscape.Business;
using Landscape.Business.FNH;
using Landscape.Business.FNH.Activities;
using Landscape.Business.Interface.LDAP;
using System.Web.Security;

namespace Landscape.WebApp
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var principle = Request.LogonUserIdentity;

            if (principle.IsAnonymous == false && principle.IsAuthenticated)
            {
                var uName = principle.Name;
                if (uName.Contains('\\'))
                {
                    uName = uName.Substring(uName.IndexOf('\\') + 1);
                    TryLogin(uName, string.Empty);
                }
            }
        }

        protected void rbtnLogin_Click(object sender, EventArgs e)
        {
            //Write the login code here
            string userName = this.rtbxUserName.Text;
            string passWord = this.rtbxPassword.Text;
            bool canLogin = TryLogin(userName, passWord);            
        }

        private bool TryLogin(string pUserName, string pPassword)
        {
            bool canLogin = false;

            using (var nhSession = NHSessionManager.GetManager().GetSession())
            {
                AdminActivities adminActivities = new AdminActivities(nhSession);
                var uMaster = adminActivities.GetUserMasterByCredentials(pUserName, true);

                if (uMaster != null)
                {
                    if (uMaster.IsDisabled)
                    {
                        this.lblMessage.Text = "This account is disabled. Please contact administrator.";
                        return false;
                    }

                    Landscape.WebApp.UIBizObjects.ActiveUser currentUser = new UIBizObjects.ActiveUser();
                    currentUser.UserID = uMaster.ID;
                    currentUser.LoginName = uMaster.Identity.LoginName;
                    currentUser.LoginIP = Request.UserHostAddress;
                    currentUser.LoginTime = DateTime.Now;
                    currentUser.IsSysAdmin = uMaster.IsSystemAdmin;
                    currentUser.IsADUser = uMaster.Identity.IsAdUser;
                    currentUser.FullName = uMaster.LastName + " " + uMaster.FirstName;

                    if (uMaster.Identity.IsAdUser)
                    {//Internal User
                        this.Session.Add("ACTIVE_USER", currentUser);
                        canLogin = true;
                    }
                    else
                    {//External User
                        if (uMaster.Identity.Password == pPassword.Trim())
                        {
                            this.Session.Add("ACTIVE_USER", currentUser);
                            canLogin = true;
                        }
                        else
                        {
                            this.lblMessage.Text = "User name or password does not matched.";
                        }
                    }
                }
                else
                {
                    this.lblMessage.Text = "The user does not exists.";
                }

                if (canLogin)
                {
                    FormsAuthentication.RedirectFromLoginPage(this.rtbxUserName.Text.Trim(), true);
                }
            }

            return canLogin;
        }
    }
}